﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MyHomeworkWebAPi.Migrations
{
    /// <inheritdoc />
    public partial class UpdateLogTableStructure : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "category",
                table: "products");

            migrationBuilder.AlterColumn<string>(
                name: "phone",
                table: "users",
                type: "nvarchar(15)",
                maxLength: 15,
                nullable: false,
                comment: "手机号",
                oldClrType: typeof(string),
                oldType: "varchar(15)",
                oldUnicode: false,
                oldMaxLength: 15,
                oldComment: "手机号");

            migrationBuilder.AlterColumn<string>(
                name: "password",
                table: "users",
                type: "nvarchar(255)",
                maxLength: 255,
                nullable: false,
                comment: "密码",
                oldClrType: typeof(string),
                oldType: "varchar(255)",
                oldUnicode: false,
                oldMaxLength: 255,
                oldComment: "密码");

            migrationBuilder.AddColumn<string>(
                name: "avatar",
                table: "users",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: true,
                comment: "头像");

            migrationBuilder.AlterColumn<string>(
                name: "title",
                table: "products",
                type: "nvarchar(100)",
                maxLength: 100,
                nullable: false,
                comment: "商品标题",
                oldClrType: typeof(string),
                oldType: "varchar(100)",
                oldUnicode: false,
                oldMaxLength: 100,
                oldComment: "商品标题");

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "products",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: false,
                comment: "状态",
                oldClrType: typeof(string),
                oldType: "varchar(50)",
                oldUnicode: false,
                oldMaxLength: 50,
                oldComment: "状态");

            migrationBuilder.AlterColumn<string>(
                name: "description",
                table: "products",
                type: "nvarchar(200)",
                maxLength: 200,
                nullable: false,
                defaultValue: "",
                comment: "描述",
                oldClrType: typeof(string),
                oldType: "varchar(200)",
                oldUnicode: false,
                oldMaxLength: 200,
                oldNullable: true,
                oldComment: "描述");

            migrationBuilder.AddColumn<Guid>(
                name: "category_id",
                table: "products",
                type: "uniqueidentifier",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
                comment: "分类ID");

            migrationBuilder.AddColumn<byte[]>(
                name: "image",
                table: "products",
                type: "varbinary(max)",
                nullable: true,
                comment: "商品图片");

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "orders",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: false,
                comment: "状态（'待付款', '已完成', '已取消', '待发货', '运输中', '待收货', '退货中', '换货中', '退款中', '部分退款', '已拒收', '售后处理中', '冻结'）",
                oldClrType: typeof(string),
                oldType: "varchar(50)",
                oldUnicode: false,
                oldMaxLength: 50,
                oldComment: "状态");

            migrationBuilder.AlterColumn<string>(
                name: "content",
                table: "comments",
                type: "nvarchar(200)",
                maxLength: 200,
                nullable: false,
                comment: "评论",
                oldClrType: typeof(string),
                oldType: "varchar(200)",
                oldUnicode: false,
                oldMaxLength: 200,
                oldComment: "评论");

            migrationBuilder.AddColumn<Guid>(
                name: "parentComment_id",
                table: "comments",
                type: "uniqueidentifier",
                nullable: true,
                comment: "父评论ID");

            migrationBuilder.CreateTable(
                name: "categories",
                columns: table => new
                {
                    category_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "分类ID"),
                    name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "分类名称"),
                    is_use = table.Column<byte>(type: "tinyint", nullable: false, comment: "是否使用")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProductCategories", x => x.category_id);
                });

            migrationBuilder.CreateTable(
                name: "interaction",
                columns: table => new
                {
                    interaction_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "互动行为id"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "用户id"),
                    product_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "商品id"),
                    type = table.Column<int>(type: "int", nullable: false, comment: "类型（1：收藏；2：浏览）"),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false, comment: "时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_interaction", x => x.interaction_id);
                    table.ForeignKey(
                        name: "FK_interaction_products",
                        column: x => x.product_id,
                        principalTable: "products",
                        principalColumn: "product_id");
                    table.ForeignKey(
                        name: "FK_interaction_users",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "messages",
                columns: table => new
                {
                    message_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    content = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    is_read = table.Column<byte>(type: "tinyint", nullable: false),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_messages", x => x.message_id);
                    table.ForeignKey(
                        name: "fk_messages_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "operation_log",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "日志ID"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: true, comment: "用户ID"),
                    type = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "操作类型"),
                    content = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false, comment: "操作描述"),
                    created_at = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())", comment: "创建时间"),
                    ip = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "IP地址")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_operation_log", x => x.id);
                    table.ForeignKey(
                        name: "FK_Logs_Users",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateIndex(
                name: "IX_products_category_id",
                table: "products",
                column: "category_id");

            migrationBuilder.CreateIndex(
                name: "IX_comments_parentComment_id",
                table: "comments",
                column: "parentComment_id");

            migrationBuilder.CreateIndex(
                name: "IX_comments_user_id",
                table: "comments",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_interaction_product_id",
                table: "interaction",
                column: "product_id");

            migrationBuilder.CreateIndex(
                name: "IX_interaction_user_id",
                table: "interaction",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "idx_messages_user_id",
                table: "messages",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_operation_log_user_id",
                table: "operation_log",
                column: "user_id");

            migrationBuilder.AddForeignKey(
                name: "FK_Comments_ParentComment",
                table: "comments",
                column: "parentComment_id",
                principalTable: "comments",
                principalColumn: "comment_id");

            migrationBuilder.AddForeignKey(
                name: "FK_comments_users_user_id",
                table: "comments",
                column: "user_id",
                principalTable: "users",
                principalColumn: "user_id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Products_Category",
                table: "products",
                column: "category_id",
                principalTable: "categories",
                principalColumn: "category_id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Comments_ParentComment",
                table: "comments");

            migrationBuilder.DropForeignKey(
                name: "FK_comments_users_user_id",
                table: "comments");

            migrationBuilder.DropForeignKey(
                name: "FK_Products_Category",
                table: "products");

            migrationBuilder.DropTable(
                name: "categories");

            migrationBuilder.DropTable(
                name: "interaction");

            migrationBuilder.DropTable(
                name: "messages");

            migrationBuilder.DropTable(
                name: "operation_log");

            migrationBuilder.DropIndex(
                name: "IX_products_category_id",
                table: "products");

            migrationBuilder.DropIndex(
                name: "IX_comments_parentComment_id",
                table: "comments");

            migrationBuilder.DropIndex(
                name: "IX_comments_user_id",
                table: "comments");

            migrationBuilder.DropColumn(
                name: "avatar",
                table: "users");

            migrationBuilder.DropColumn(
                name: "category_id",
                table: "products");

            migrationBuilder.DropColumn(
                name: "image",
                table: "products");

            migrationBuilder.DropColumn(
                name: "parentComment_id",
                table: "comments");

            migrationBuilder.AlterColumn<string>(
                name: "phone",
                table: "users",
                type: "varchar(15)",
                unicode: false,
                maxLength: 15,
                nullable: false,
                comment: "手机号",
                oldClrType: typeof(string),
                oldType: "nvarchar(15)",
                oldMaxLength: 15,
                oldComment: "手机号");

            migrationBuilder.AlterColumn<string>(
                name: "password",
                table: "users",
                type: "varchar(255)",
                unicode: false,
                maxLength: 255,
                nullable: false,
                comment: "密码",
                oldClrType: typeof(string),
                oldType: "nvarchar(255)",
                oldMaxLength: 255,
                oldComment: "密码");

            migrationBuilder.AlterColumn<string>(
                name: "title",
                table: "products",
                type: "varchar(100)",
                unicode: false,
                maxLength: 100,
                nullable: false,
                comment: "商品标题",
                oldClrType: typeof(string),
                oldType: "nvarchar(100)",
                oldMaxLength: 100,
                oldComment: "商品标题");

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "products",
                type: "varchar(50)",
                unicode: false,
                maxLength: 50,
                nullable: false,
                comment: "状态",
                oldClrType: typeof(string),
                oldType: "nvarchar(50)",
                oldMaxLength: 50,
                oldComment: "状态");

            migrationBuilder.AlterColumn<string>(
                name: "description",
                table: "products",
                type: "varchar(200)",
                unicode: false,
                maxLength: 200,
                nullable: true,
                comment: "描述",
                oldClrType: typeof(string),
                oldType: "nvarchar(200)",
                oldMaxLength: 200,
                oldComment: "描述");

            migrationBuilder.AddColumn<string>(
                name: "category",
                table: "products",
                type: "varchar(50)",
                unicode: false,
                maxLength: 50,
                nullable: false,
                defaultValue: "",
                comment: "分类");

            migrationBuilder.AlterColumn<string>(
                name: "status",
                table: "orders",
                type: "varchar(50)",
                unicode: false,
                maxLength: 50,
                nullable: false,
                comment: "状态（'待付款', '已完成', '已取消', '待发货', '运输中', '待收货', '退货中', '换货中', '退款中', '部分退款', '已拒收', '售后处理中', '冻结'）",
                oldClrType: typeof(string),
                oldType: "nvarchar(50)",
                oldMaxLength: 50,
                oldComment: "状态");

            migrationBuilder.AlterColumn<string>(
                name: "content",
                table: "comments",
                type: "varchar(200)",
                unicode: false,
                maxLength: 200,
                nullable: false,
                comment: "评论",
                oldClrType: typeof(string),
                oldType: "nvarchar(200)",
                oldMaxLength: 200,
                oldComment: "评论");
        }
    }
}
